package com.caigou.modules.service.ui

import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.container.BaseFragment
import com.caigou.modules.service.databinding.ActivityServiceBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/10
 */
@Route(path = RouterPath.Service.PAGE_SERVICE)
@AndroidEntryPoint
class ServiceActivity : BaseActivity() {
    private lateinit var binding: ActivityServiceBinding
    private var websiteCollectFragment: BaseFragment? = null
    private var platformFragment: BaseFragment? = null
    private var helpFragment: BaseFragment? = null

    @Autowired(name = AppConstants.TARGET_FRAGMENT_TAG)
    lateinit var targetFragmentTag: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityServiceBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.serviceFlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        switchFragment(targetFragmentTag)
    }

    override fun switchFragment(tag: String) {
        when (tag) {
            RouterPath.Service.TAG_WEBSITE_COLLECT -> {
                if (websiteCollectFragment == null) {
                    websiteCollectFragment = WebsiteCollectFragment()
                }
                switchFragment(websiteCollectFragment!!, tag)
            }
            RouterPath.Service.TAG_PLATFORM -> {
                if (platformFragment == null) {
                    platformFragment = PlatformFragment()
                }
                switchFragment(platformFragment!!, tag)
            }
            RouterPath.Service.TAG_HELP -> {
                if (helpFragment == null) {
                    helpFragment = HelpFragment()
                }
                switchFragment(helpFragment!!, tag)
            }
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            if (it.action == MotionEvent.ACTION_DOWN) {
                val v = currentFocus
                if (isShouldHideInput(v, it)) {
                    val imm =
                        this@ServiceActivity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v!!.windowToken, 0)
                }
                return super.dispatchTouchEvent(it)
            }
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

    override fun onBackPressed() {
        if (fragmentStack.isNotEmpty() && fragmentStack.size > 1) {
            fragmentStack.removeLast()
            switchFragment(fragmentStack.last)
        } else {
            super.onBackPressed()
        }
    }
}